package com.alibaba.pet.controller;

import com.alibaba.basic.jwt.UserInfo;
import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.LoginContext;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.dto.AcceptDto;
import com.alibaba.pet.dto.PublishDto;
import com.alibaba.pet.query.SearchMasterMsgQuery;
import com.alibaba.pet.service.ISearchMasterMsgService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    public IUserService userService;
    @Autowired
    public EmployeeMapper employeeMapper;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.finById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
     * 前台分页查询数据
     * 这个接口是用户在个人中心查看自己发布的所有消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/front/list")
    public PageList<SearchMasterMsg> frontList(@RequestBody SearchMasterMsgQuery query)
    {
        UserInfo userInfo = LoginContext.getUserInfo();
        User user = userService.findByLogininfoId(userInfo.getLogininfo().getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     *
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody PublishDto dto)
    {
        searchMasterMsgService.publish(dto);
        return AjaxResult.me().success();
    }
    /**
     * 店铺查询 待处理数据 寻主消息 自动匹配到商家
     * @param query
     * @return
     */

    @PostMapping("/tohandle")
    public PageList<SearchMasterMsg> tohandle(@RequestBody SearchMasterMsgQuery query)
    {
        // 获取当前店铺id 审核成功 店铺id=shopId
        Logininfo logininfo = LoginContext.getLogininfo();
        Employee byLogininfoId = employeeMapper.findByLogininfoId(logininfo.getId());
        query.setShopId(byLogininfoId.getShopId());
        query.setState(1);
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 寻主池 寻主消息未匹配到商家
     * @param query
     * @return
     */

    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        // 审核成功 店铺id = null
        query.setState(1);
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 已完成 商家对寻主 已经进行收购
     * @param query
     * @return
     */

    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query)
    {
        // 状态为已完成 state = 1 & 店铺id = shopId
        Logininfo  logininfo = LoginContext.getLogininfo();
        Employee byLogininfoId = employeeMapper.findByLogininfoId(logininfo.getId());
        query.setShopId(byLogininfoId.getShopId());
        query.setState(4);
        return searchMasterMsgService.queryByPage(query);
    }


    /**
     * 拒单
     * @param query
     * @return
     */

    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable Long id) {
        searchMasterMsgService.reject(id);
        return AjaxResult.me().success();

    }
    /**
     * 接单
     * @param
     * @return
     */

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto) {
        searchMasterMsgService.accept(dto);
        return AjaxResult.me().success();
    }
}
